package cn.hrlly.tao2shou.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.Commodity;
import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.presenter.CommodityDetailsPresenter;
import cn.hrlly.tao2shou.presenter.CommodityDetailsPresenterImpl;
import cn.hrlly.tao2shou.utils.GpsUtil;
import cn.hrlly.tao2shou.view.base.BaseActivity;
import cn.hrlly.tao2shou.view.view_interface.CommodityDetailsView;

public class CommodityDetailsActivity extends BaseActivity implements CommodityDetailsView {

    @Bind(R.id.viewPager)
    ViewPager viewPager;
    @Bind(R.id.tvCommodityName)
    TextView tvCommodityName;
    @Bind(R.id.tvPrice)
    TextView tvPrice;
    @Bind(R.id.ivBarterFlag)
    ImageView ivBarterFlag;
    @Bind(R.id.ratingBar)
    RatingBar ratingBar;
    @Bind(R.id.tvClassification)
    TextView tvClassification;
    @Bind(R.id.tvExpectedTime)
    TextView tvExpectedTime;
    @Bind(R.id.tvExpectedLocation)
    TextView tvExpectedLocation;
    @Bind(R.id.tvReleaseTime)
    TextView tvReleaseTime;
    @Bind(R.id.tvBrowseTimes)
    TextView tvBrowseTimes;
    @Bind(R.id.tvDistance)
    TextView tvDistance;
    @Bind(R.id.tvCommodityCount)
    TextView tvCommodityCount;
    @Bind(R.id.tvCommodityDescription)
    TextView tvCommodityDescription;
    @Bind(R.id.tvBarterDescription)
    TextView tvBarterDescription;
    @Bind(R.id.layout_barter)
    LinearLayout layoutBarter;
    @Bind(R.id.btnApplication)
    Button btnApplication;
    @Bind(R.id.btnSeller)
    Button btnSeller;
    private CommodityDetailsPresenter commodityDetailsPresenter;
    private Intent intent;
    private Commodity commodity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity_details);
        //绑定控件
        ButterKnife.bind(this);
        //初始化
        commodityDetailsPresenter=new CommodityDetailsPresenterImpl(this,this);
        intent=getIntent();
        //联网获取商品详情
        commodityDetailsPresenter.showCommodityDetails();
    }

    //关闭数据库
    @Override
    protected void onDestroy() {
        super.onDestroy();
        commodityDetailsPresenter.closeDataBase();
    }

    //btnApplication点击事件
    @OnClick(R.id.btnApplication)
    public void application(){
        Intent intent =new Intent(CommodityDetailsActivity.this,SubmitApplicationActivity.class);
        intent.putExtra(Config.key_release_id,getReleaseId());
        intent.putExtra(Config.key_trade_method,commodity.getBarterFlag());
        intent.putExtra(Config.key_seller_id,commodity.getSellerId());
        startActivity(intent);
    }

    //btnSeller点击事件
    @OnClick(R.id.btnSeller)
    public void seller(){
        //这里给天鑫写,跳转到聊天界面
    }

    //以下是对activity的操作
    @Override
    public String getReleaseId() {
        return intent.getStringExtra(Config.key_release_id);
    }

    @Override
    public void showCommodityDetails(Commodity commodity) {
        this.commodity=commodity;
        if(commodity.getBarterFlag()==0){
            ivBarterFlag.setVisibility(View.VISIBLE);
            layoutBarter.setVisibility(View.VISIBLE);
            tvBarterDescription.setText(commodity.getBarterDescription());
        }
        //viewPager操作
        tvCommodityName.setText(commodity.getCommodityName());
        tvClassification.setText(commodity.getClassification());
        tvPrice.setText(commodity.getExpectedPrice()+"元");
        ratingBar.setRating(commodity.getNewDegree());
        tvCommodityDescription.setText(commodity.getCommodityDescription());
        tvExpectedTime.setText(commodity.getExpectedTime());
        tvExpectedLocation.setText(commodity.getExpectedLocation());
        tvReleaseTime.setText(commodity.getReleaseTime());
        tvDistance.setText(commodity.getDistance()+"米");
        tvCommodityCount.setText(commodity.getCommodityCount()+"个");
        tvBrowseTimes.setText(commodity.getBrowseTimes()+"次");
    }

    @Override
    public void showMessage(String errorMessage) {
        Toast.makeText(this,errorMessage,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showMessage(int errorMessage) {
        Toast.makeText(this,errorMessage,Toast.LENGTH_SHORT).show();
    }
}
